package main

import (
	"flag"
	"jkgo/jk/jklog"
	"jkgo/jkbase"
	"findService/ss/jkclient/jkclient"
	"time"

	l4g "github.com/alecthomas/log4go"
)

var (
	cmd   = flag.String("cmd", "run", "run/install/remove service")
	conf  = flag.String("conf", "etc/findyouwant/jkclient.json", "config file")
	debug = flag.Bool("d", false, "debug mode")
)

type clientArgs struct {
	jkbase.ConfigBaseArgs
}

var client_args clientArgs

func start() {
	jkclient.NewClientIoT(client_args.ClientAddr, client_args.ClientPort)
}

func Start(addr string, port int) error {
	c, err := jkclient.NewDemoClient(client_args.ClientAddr, client_args.ClientPort)
	if err != nil {
		l4g.Debug("create demo client failed ", err)
		return err
	}

	go c.KeepaliveCycle(30, "jkclient")
	for {
		c.DealRecv()
		time.Sleep(1 * time.Second)
	}
	return nil
}

func main() {
	flag.Parse()

	err := jkbase.CMConfigFile(*conf, &client_args)
	if err != nil {
		jklog.L().Errorln(err)
		return
	}
	jklog.L().InitLog(client_args.LogFile)
	if *debug {
		jklog.L().SetLevel(jklog.LEVEL_ALL)
	}
	jklog.L().Infoln("jkclient program started...")

	_, err = jkbase.InitProgramRun("jkclient", "jkclient program", start)
	if err != nil {
		jklog.L().Errorln(err)
		return
	}

	jklog.L().Errorln("Program exit...")
}
